package jwlee.testdb;

//解决连接MySQL的中文乱码问题

import java.io.UnsupportedEncodingException;
import java.sql.*;

/**
 * 封装了对数据库的连接信息
 */
public class DBConnector {

    private String driver;

    private String url;

    private String user;

    private String password;

    public DBConnector() {
    }

    public DBConnector(String driver, String url, String user, String password) {
        this.driver = driver;
        this.url = url;
        this.user = user;
        this.password = password;
    }

    /**
     * @return 返回 driver。
     */
    public String getDriver() {
        return driver;
    }

    /**
     * @param driver
     *            要设置的 driver。
     */
    public void setDriver(String driver) {
        this.driver = driver;
    }

    /**
     * @return 返回 password。
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param password
     *            要设置的 password。
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * @return 返回 url。
     */
    public String getUrl() {
        return url;
    }

    /**
     * @param url
     *            要设置的 url。
     */
    public void setUrl(String url) {
        this.url = url;
    }

    /**
     * @return 返回 user。
     */
    public String getUser() {
        return user;
    }

    /**
     * @param user
     *            要设置的 user。
     */
    public void setUser(String user) {
        this.user = user;
    }

    public boolean testConnect() {
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return false;
        }
        try {
            DriverManager.getConnection(url, user, password);
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 获得对数据库的连接
     * 
     * @return 对数据库的连接
     */
    public Connection getConnection() {
        Connection conn = null;
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        try {
            conn = DriverManager.getConnection(url, user, password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }

    public static void main(String args[]) {
        DBConnector dbc = new DBConnector("com.mysql.jdbc.Driver",
                "jdbc:mysql://localhost/test", "root", "123456");
        Connection conn = dbc.getConnection();
        Statement stmt = null;
        try {
            stmt = conn.createStatement();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        String s = null;
        try {
            s = new String("你好".getBytes("GB2312"), "ISO-8859-1");
        } catch (UnsupportedEncodingException e2) {
            e2.printStackTrace();
        }
        String sql1 = "insert into table1 values('" + s + "')";
        // System.out.println("sql1:" + sql1);
        String sql2 = "select * from table1";
        String sql3 = "show tables from fd";
        ResultSet rs = null;
        try {
            int row = 0;
            row = stmt.executeUpdate(sql1);
            System.out.println(row);

            rs = stmt.executeQuery(sql2);
            while (rs.next()) {
                String result = rs.getString("value");
                result = new String(result.getBytes("ISO-8859-1"), "GB2312");
                System.out.println(result);
            }

            rs = stmt.executeQuery(sql3);
            while (rs.next()) {
                String result = rs.getString(1);
                // result = new String(result.getBytes("ISO-8859-1"), "GB2312");
                System.out.println(result);
            }
        } catch (SQLException e1) {
            e1.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        try {
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
